package com.junlong0716.enjoylearning.ui

import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ToastUtils
import com.htxcsoft.corelibrary.glide.GlideUtils
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.SettingActivity
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.entity.rxevent.RefreshAvatarEvent
import com.junlong0716.module.common.base.BaseFragment
import com.junlong0716.module.common.rxbus.RxBus
import com.junlong0716.module.common.rxbus.Subscribe
import com.junlong0716.module.common.rxbus.ThreadMode
import de.hdodenhof.circleimageview.CircleImageView

/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午9:18 2018/4/22
 *@modified by:
 */
class MineFragment : BaseFragment<MinePresenter>(), MineContract.View, View.OnClickListener {

    private lateinit var citAvatar: CircleImageView

    override fun getLayoutId(): Int = R.layout.fragment_mine


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun eventBus(s: RefreshAvatarEvent) {
        GlideUtils.showImage(activity!!, NetConstant.REQUEST_SOURCE + MyApp.instance().userAccount!!.headImg, citAvatar, R.mipmap.ic_avatar_man)
    }

    override fun initViews(mRootView: View?) {
        RxBus.getDefault().register(this)

        mRootView!!.findViewById<RelativeLayout>(R.id.rl_setting).setOnClickListener(this)
        mRootView.findViewById<RelativeLayout>(R.id.rl_download).setOnClickListener(this)
        mRootView.findViewById<RelativeLayout>(R.id.rl_attention).setOnClickListener(this)
        mRootView.findViewById<RelativeLayout>(R.id.rl_about_us).setOnClickListener(this)
        mRootView.findViewById<RelativeLayout>(R.id.rl_feedback).setOnClickListener(this)
        mRootView.findViewById<RelativeLayout>(R.id.rl_info_mine).setOnClickListener(this)
        mRootView.findViewById<RelativeLayout>(R.id.rl_message).setOnClickListener(this)

        citAvatar = mRootView.findViewById<CircleImageView>(R.id.civ_avatar)
        GlideUtils.showImage(activity!!, NetConstant.REQUEST_SOURCE + MyApp.instance().userAccount!!.headImg, citAvatar, R.mipmap.ic_avatar_man)
        mRootView.findViewById<TextView>(R.id.tv_nickname).text = MyApp.instance().userAccount!!.userName

    }

    override fun lazyFetchData() {
    }

    override fun attachPresenter() {
        mPresenter = MinePresenter()
        mPresenter!!.attachView(this)
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.rl_message -> {
                go(MessageActivity::class.java)
            }
            R.id.rl_setting -> {
                go(SettingActivity::class.java)
            }

            R.id.rl_download -> {
                go(DownloadActivity::class.java)
            }

            R.id.rl_about_us -> {
                ToastUtils.showShort("享学")
                //go(AboutUsActivity::class.java)
            }

            R.id.rl_feedback -> {
                go(FeedbackActivity::class.java)
            }

            R.id.rl_attention -> {
                go(StudyCollectionActivity::class.java)
            }

            R.id.rl_info_mine -> {
                go(UserInfoActivity::class.java)
            }

            else -> {
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        RxBus.getDefault().unregister(this)
    }
}